﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Planer.Model.Validators.Base;
using Planer.Model;

namespace Planer.UI.Forms
{
    public partial class NewPasswordForm : Form
    {
        user _user = null;

        public NewPasswordForm(user User)
        {
            InitializeComponent();
            _user = User;
        }

        public NewPasswordForm()
        {
            InitializeComponent();
        }

        public string NewPassword
        {
            get
            {
                return textBoxNewPass.Text;
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (textBoxOldPass.Text != Program.CurrentUser.password_hash)
            {
                MessageBoxUtils.ShowErrorBox("Введен неверный текущий пароль");
                return;
            }
            PasswordValidator pv = new PasswordValidator();
            pv.Validate(textBoxNewPass.Text, textBoxConfirmPass.Text);
            if (pv.Status == OperationStatusType.ERROR)
            {
                MessageBoxUtils.ShowErrorBox("");
            }
            
            _user.password_hash = NewPassword;
            ModelResult mr = UserModel.GetInstance().SaveOrUpdateUser(_user);
            if (mr.Status == OperationStatusType.ERROR)
            {
                MessageBoxUtils.ShowErrorBox(mr.FirstError);
                _user.password_hash = textBoxOldPass.Text;
                return;
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
    }
}
